// 引入样式
//判断是否有token
checkLogin()
// 渲染用户名
renderUername()
// 退出登录
logout()
async function getData() {

  const token = localStorage.getItem('token')
  const res = await axios.get('/dashboard', {
  })
  console.log(res.data)

  const { overview, year, salaryData, groupData,provinceData } = res.data
  rnd(overview)
  renderUername1(year)
  renderUername2(salaryData)
  renderUername3(groupData)
  renderUername4(salaryData)
  renderProvince(provinceData)
}
getData()





function rnd(overview) {
  Object.keys(overview).forEach(key => {
    document.querySelector(`.${key}`).innerHTML = overview[key]
  })
}

function renderUername1(year) {
  // 基于准备好的dom，初始化echarts实例
  const myChart = echarts.init(document.querySelector('#line'))
  // 指定图表的配置项和数据
  const option = {
    title: {
      text: '2022年全年学科薪资走势',
      left: 20,
      top: 20
    },
    grid: {
      top: '20%'
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      data: year.map(item => item.month),
      axisLine: {
        lineStyle: {
          type: 'dashed',
          color: '#999'
        }
      }
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
    },
    series: [
      {
        // data: [820, 932, 901, 934, 1290, 1330, 1320],
        data: year.map(item => item.salary),
        type: 'line',
        smooth: true,
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#75ABF2' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(255,255,255,0)' // 100% 处的颜色
            }],
          }
        },
        lineStyle: {
          width: 7,
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 1,
            y2: 0,
            colorStops: [{
              offset: 0, color: '#75ABF2' // 0% 处的颜色
            }, {
              offset: 1, color: '#CCC' // 100% 处的颜色
            }],
          }
        },
        symbolSize: 10,

      }
    ]
  };
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option)
}
function renderUername2(salaryData) {
  // 基于准备好的dom，初始化echarts实例
  const myChart = echarts.init(document.querySelector('#salary'))
  // 指定图表的配置项和数据
  const option = {
    title: {
      text: '班级薪资分布',
      left: 15,
      top: 15
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: '5%',
      left: 'center'
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: ['55%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 5
        },
        label: {
          show: false,
          // position: 'center'
        },
        // emphasis: {
        //   label: {
        //     show: true,
        //     fontSize: 40,
        //     fontWeight: 'bold'
        //   }
        // },
        labelLine: {
          show: false
        },
        data: salaryData.map(item => {
          return {
            name: item.label,
            value: item.g_count + item.b_count
          }
        })
      }
    ]
  };
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option)
}


function renderUername3(groupData) {
  // 基于准备好的dom，初始化echarts实例
  const myChart = echarts.init(document.querySelector('#lines'))
  // 指定图表的配置项和数据
  const option = {
    xAxis: {
      type: 'category',
      data: groupData[1].map(item => item.name),
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '期望薪资',
        data: groupData[1].map(item => item.hope_salary),
        type: 'bar',
        itemStyle: {
          width: 7,
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#75ABF2' // 0% 处的颜色
            }, {
              offset: 1, color: '#CCC' // 100% 处的颜色
            }],
          }
        },
      },
      {
        name: '实际薪资',
        data: groupData[1].map(item => item.salary),
        type: 'bar',
        itemStyle: {
          width: 7,
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#75ABF2' // 0% 处的颜色
            }, {
              offset: 1, color: '#CCC' // 100% 处的颜色
            }],
            global: false,
          }
        },
      }
    ],
    tooltip: {
      trigger: 'item',

    }
  };
  myChart.setOption(option)
  document.querySelector('#btns').addEventListener('click', function (e) {
    if (e.target.classList.contains('btn')) {
      document.querySelector('.btn-blue').classList.remove('btn-blue')
      e.target.classList.add('btn-blue')


      const num = e.target.innerHTML
      option.series[0].data = groupData[num].map(item => item.hope_salary)
      option.series[1].data = groupData[num].map(item => item.salary)
      option.xAxis.data = groupData[num].map(item => item.name)
      myChart.setOption(option)


    }
  })
}
function renderUername4(salaryData) {
  // 基于准备好的dom，初始化echarts实例
  const myChart = echarts.init(document.querySelector('#gender'))
  // 指定图表的配置项和数据
  const option = {
    title: [{
      text: '男女薪酬分布',
      left: 15,
      top: 15,
      textStyle: {
        fontSize: 16
      }
    }, {
      text: '男生',
      left: '45%',
      top: '45%',
      textStyle: {
        fontSize: 12
      }
    },
    {
      text: '女生',
      left: '45%',
      top: '85%',
      textStyle: {
        fontSize: 12
      }
    }
    ],
    tooltip: {
      trigger: 'item'
    },
    // legend: {
    //   top: '5%',
    //   left: 'center'
    // },

    series: [
      {
        center: ['50%', '30%'],
        name: '男生',
        type: 'pie',
        radius: ['20%', '30%'],
        avoidLabelOverlap: false,
        label: {
          show: true,
          // position: 'center'
        },
        // emphasis: {
        //   label: {
        //     show: true,
        //     fontSize: 40,
        //     fontWeight: 'bold'
        //   }
        // },
        labelLine: {
          show: true
        },
        data: salaryData.map(item => ({
          name: item.label,
          value: item.b_count
        }))
      },
      {
        center: ['50%', '70%'],
        name: '女生',
        type: 'pie',
        radius: ['20%', '30%'],
        avoidLabelOverlap: false,
        label: {
          show: true,
          // position: 'center'
        },
        // emphasis: {
        //   label: {
        //     show: true,
        //     fontSize: 40,
        //     fontWeight: 'bold'
        //   }
        // },
        labelLine: {
          show: true
        },
        data: salaryData.map(item => ({
          name: item.label,
          value: item.g_count
        }))
      }
    ]
  };
  myChart.setOption(option)

}

// 渲染籍贯分布
function renderProvince(provinceData) {
  // console.log(provinceData)
  const dom = document.querySelector('#map')
  const myEchart = echarts.init(dom)
  const dataList = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 },
  ]

  // 筛选数据
  dataList.forEach(item => {
    const res = provinceData.find(v => {
      return v.name.includes(item.name)
    })
    // console.log(res)
    // 数据赋值
    if (res !== undefined) {
      item.value = res.value
    }
  })
  // console.log(dataList)

  const option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff',
      },
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0'],
      },
      show: true,
      left: 40,
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)',
        },
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff',
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: dataList,
      },
    ],
  }
  myEchart.setOption(option)
}
